//
//  NoticeTableViewCell.swift
//  BellManage
//
//  Created by Bestsch on 16/3/31.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class NoticeTableViewCell: UITableViewCell {

    var bgView, photoBgView,typeMark: UIView!
    var contentType,titleLabel,contentLabel,nameLabel,timeLabel: UILabel!
    var oneImage: UIImageView!
    var photoCollectionView: UICollectionView!
    var deleImage: UIImageView! // 全校通知中增加删除按钮
    var dic: JSON!
    
    let collectionIdentifier = "PhotoCollectionViewCellIdentifier"
    let imageArray = NSMutableArray()
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    init(dictionary: JSON, reuseIdentifier: String?) {
        self.dic = dictionary
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: reuseIdentifier)
        setUp()
    }
    
    /**
     初始化设置各种控件
     */
    func setUp() {
        
        bgView = UIView.init(frame: CGRect.zero)
        bgView.backgroundColor = UIColor.init(hexString: "f6f6f6", alpha: 1)

        timeLabel = UILabel.init(frame: CGRect.zero)
        timeLabel.font = UIFont.boldSystemFont(ofSize: 14)
        timeLabel.textColor = UIColor.darkText
        timeLabel.textAlignment = NSTextAlignment.left
        
        nameLabel = UILabel.init(frame: CGRect.zero)
        nameLabel.font = UIFont.systemFont(ofSize: 14)
        nameLabel.textColor = UIColor.init(red: 0.310, green: 0.376, blue: 0.510, alpha: 1.00)
        nameLabel.textAlignment = NSTextAlignment.left
        
        photoBgView = UIView.init(frame: CGRect.zero)
        photoBgView.backgroundColor = UIColor.white

        contentType = UILabel.init(frame: CGRect.zero)
        contentType.font = UIFont.systemFont(ofSize: 14)
        contentType.textColor = barColor
        contentType.textAlignment = NSTextAlignment.right
        
        typeMark = UIView.init(frame: CGRect.zero)
    
        contentLabel = UILabel.init(frame: CGRect.zero)
        
        contentLabel.textAlignment = NSTextAlignment.left
        
        deleImage = UIImageView.init(frame: CGRect.zero)
        deleImage.image = #imageLiteral(resourceName: "del")
        deleImage.contentMode = UIViewContentMode.scaleAspectFit
        deleImage.isUserInteractionEnabled = true
        let deleTap = UITapGestureRecognizer.init(target: self, action: #selector(NoticeTableViewCell.deleSchNotice))
        deleImage.addGestureRecognizer(deleTap)
        
        var title = dic["title"].stringValue
        title = title.removingPercentEncoding!
        var time = dic["datetime"].stringValue
        let imageUrl = dic["fileurl"].stringValue
        let name = dic["teaname"].stringValue
        let type = dic["types"].stringValue
        
        
        let format = DateFormatter()
        let formatString = "yyyy-MM-dd HH:mm:ss"
        format.dateFormat = formatString
        let array: NSArray = time.components(separatedBy: ".") as NSArray
        var time1 = array.object(at: 0) as! String
        time1 = time1.replacingOccurrences(of: "T", with: " ")
        let date = format.date(from: time1 )
        format.dateFormat = "yyyy年MM月dd日"
        time = format.string(from: date!)
        if type == "7" {
            //全校通知
            let titleStr = "【校级通知】\(title)"
            let str: NSMutableAttributedString = NSMutableAttributedString.init(string: titleStr)
            str.addAttribute(NSForegroundColorAttributeName, value: UIColor.init(hexString: "e50113", alpha: 1), range: NSMakeRange(0, 6))
            str.addAttribute(NSForegroundColorAttributeName, value: textlight, range: NSMakeRange(6, str.length - 6))
            str.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 14), range: NSMakeRange(0, 6))
            str.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(6, str.length - 6))
            contentLabel.attributedText = str

        }else if type == "6" {
            //系统通知
            let titleStr = "【系统通知】\(title)"
            let str: NSMutableAttributedString = NSMutableAttributedString.init(string: titleStr)
            str.addAttribute(NSForegroundColorAttributeName, value: UIColor.init(hexString: "4e92df", alpha: 1), range: NSMakeRange(0, 6))
            str.addAttribute(NSForegroundColorAttributeName, value: textlight, range: NSMakeRange(6, str.length - 6))
            str.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 14), range: NSMakeRange(0, 6))
            str.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(6, str.length - 6))
            contentLabel.attributedText = str
        }else {
            contentLabel.font = UIFont.systemFont(ofSize: 14)
            contentLabel.textColor = textlight
            contentLabel.text = title
        }
        
        timeLabel.text = time
        nameLabel.text = "通知人 " + name
        
        self.contentView.addSubview(bgView)
        self.contentView.addSubview(photoBgView)
        self.contentView.addSubview(typeMark)
        self.contentView.addSubview(contentType)
        self.contentView.addSubview(contentLabel)
        self.contentView.addSubview(timeLabel)
        self.contentView.addSubview(nameLabel)
        self.contentView.addSubview(deleImage)
        
        if imageUrl.characters.count == 0 {
            
            contentType.textColor = UIColor.init(red: 0.992, green: 0.544, blue: 0.149, alpha: 1)
            contentType.text = "文本通知"
            typeMark.backgroundColor = UIColor.init(red: 0.992, green: 0.544, blue: 0.149, alpha: 1)
          
            
        }else {
            
            contentType.textColor = UIColor.init(red: 0.114, green: 0.804, blue: 0.494, alpha: 1)
            typeMark.backgroundColor = UIColor.init(red: 0.114, green: 0.804, blue: 0.494, alpha: 1)
            contentType.text = "图文通知"
        }
        
        bgView.frame = CGRect(x: 10, y: 10, width: screenW - 20, height: 110)
        bgView.layer.cornerRadius = 4;
        bgView.layer.shadowOffset = CGSize(width: 0, height: 1);  // 设置阴影的偏移量
        bgView.layer.shadowRadius = 2.0;  // 设置阴影的半径
        bgView.layer.shadowColor = UIColor.lightGray.cgColor; // 设置阴影的颜色为黑色
        bgView.layer.shadowOpacity = 0.8; // 设置阴影的不透明度
        
        timeLabel.frame = CGRect(x: bgView.frame.minX + 10, y: bgView.frame.minY, width: screenW/2, height: 30)
        //类型
        typeMark.frame = CGRect(x: bgView.frame.maxX-15, y: bgView.frame.minY + 5, width: 2, height: 20);
        contentType.frame = CGRect(x: typeMark.frame.minX-110, y: bgView.frame.minY, width: 100, height: 30);
        photoBgView.frame = CGRect(x: bgView.frame.minX, y: timeLabel.frame.maxY, width: bgView.frame.width, height: 50)
        contentLabel.frame = CGRect(x: bgView.frame.minX + 10, y: timeLabel.frame.maxY, width: bgView.frame.width - 20, height: 50)
        nameLabel.frame = CGRect(x: bgView.frame.minX + 10, y: photoBgView.frame.maxY, width: screenW/2, height: 30)

        deleImage.frame = CGRect(x: bgView.frame.maxX - 40, y: bgView.frame.maxY - 25, width: 20, height: 20)
        
    }
    
    func deleSchNotice() {
        NotificationCenter.default.post(name: Notification.Name(rawValue: "SchNoticeDelegateNotification"), object: nil, userInfo: nil)
    
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
